package com.dwjm.goods;

import com.dwjm.CommonResult;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/queryByCondition")
public class QueryByCondition extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String keywords = req.getParameter("keywords");
        GoodDaolpml goodDao = new GoodDaolpml();
        List<Goods> goodsByKeywords = goodDao.queryByCondition(keywords);
        Gson gson = new Gson();

        CommonResult<List<Goods>> success = CommonResult.success(goodsByKeywords);
        String s = gson.toJson(success);
        if (goodsByKeywords.size() > 0) {
            resp.getWriter().write(s);
        }
        else {
            resp.getWriter().write("N");
        }
    }
}
